using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using Unity.VisualScripting;
using UnityEngine;
using JSG.BubbleShoot;

namespace JSG.BubbleShoot
{
    public class LevelBase : MonoBehaviour
    {
        public GameObject m_BallPrefab;
        public EditorRing[] m_Rings;
        public List<EditorBallBase> m_Balls;

        public Sprite[] m_BallSprites;
        public Sprite m_BombSprite;

        public float m_StartAngle = 0;
        public float m_DeltaAngle = 90;

        public float m_StartRadius = 40;

        public bool m_Generated = false;

        public void GenerateRings()
        {
            int ringCount = 6;
            m_Rings = new EditorRing[ringCount];
            float startRadius = 40;
            float delta = 5;
            for (int i = 0; i < ringCount; i++)
            {
                GameObject obj = new GameObject("ring-" + i.ToString());
                obj.transform.SetParent(transform, false);
                EditorRing ring = obj.AddComponent<EditorRing>();
                ring.m_Radius = startRadius;
                startRadius += 6.5f;
                //delta -= .14f;
                m_Rings[i] = ring;
            }
        }

        public void GenerateBalls()
        {
            m_Balls = new List<EditorBallBase>();
            float startRadius = 30;
            float delta = 7f;

            for (int i = 0; i < 6; i++)
            {
                float m_SpawnCount = (2 * Mathf.PI * startRadius) / 7;
                int count = (int)m_SpawnCount;
                if (count % 2 == 1)
                {
                    count--;
                }

                float m_SpawnAngle = 360f / (float)count;


                float start = (i % 2) * (m_SpawnAngle / 2f);

                for (int j = 0; j < count; j++)
                {
                    Vector3 cordinates = Quaternion.Euler(0, 0, start + j * m_SpawnAngle) * new Vector3(startRadius, 0, 0);
                    GameObject obj = Instantiate(m_BallPrefab, cordinates, Quaternion.identity);
                    obj.transform.SetParent(transform, false);
                    obj.name = "Ball-" + i.ToString() + "-" + j.ToString();
                    EditorBallBase b = obj.GetComponent<EditorBallBase>();
                    b.m_ColorSprites = m_BallSprites;
                    b.m_BombSprite = m_BombSprite;
                    b.SetColor(0);
                    b.SetType(0);
                    m_Balls.Add(b);
                }
                startRadius += delta;
            }
        }

        public void FillAllRings()
        {
            for (int i = 0; i < m_Rings.Length; i++)
            {
                FillRings(i);
            }
        }

        public void FillAllRings2()
        {
            for (int i = 0; i < m_Rings.Length; i++)
            {
                m_Rings[i].m_Radius = 40 + i * 6.5f;
                FillRings2(i);
            }
        }

        public void FillRings(int ringNum)
        {
            EditorRing ring = m_Rings[ringNum];
            float radius = ring.m_Radius;

            //float m_SpawnCount = (2 * Mathf.PI * radius) / 7;
            float m_SpawnCount = (2 * Mathf.PI * 40) / 10;
            float m_SpawnAngle = (360 / (float)Math.Floor(m_SpawnCount));

            int count = (int)m_SpawnCount;
            ring.m_Balls = new EditorBallBase[count];

            float start = (ringNum % 2) * (m_SpawnAngle / 2f);

            for (int i = 0; i < count; i++)
            {
                Vector3 cordinates = Quaternion.Euler(0, 0, start + i * m_SpawnAngle) * new Vector3(radius, 0, 0);
                GameObject obj = Instantiate(m_BallPrefab, cordinates, Quaternion.identity);
                obj.transform.SetParent(ring.transform, false);
                ring.m_Balls[i] = obj.GetComponent<EditorBallBase>();

                ring.m_Balls[i].transform.localScale = Vector3.one + (.04f * ringNum) * Vector3.one;
                //float delta = i * m_SpawnAngle+80*start;
                float delta = i * m_SpawnAngle;
                int num = (int)(delta / 40f);
                num = num % 5;
                ring.m_Balls[i].SetColor(num);
                //m_ColorCount[num]++;

                //ring.GetComponent<Ring>().m_Candies.Add(obj.GetComponent<Candy>());
            }
        }

        public void FillRings2(int ringNum)
        {
            EditorRing ring = m_Rings[ringNum];
            float radius = ring.m_Radius;

            float m_SpawnCount = (2 * Mathf.PI * radius) / 6.5f;
            m_SpawnCount = (float)Math.Floor(m_SpawnCount);
            if (m_SpawnCount % 2 == 1)
            {
                m_SpawnCount--;
            }
            //float m_SpawnCount = (2 * Mathf.PI * 40) / 10;
            float m_SpawnAngle = (360f / m_SpawnCount);

            int count = (int)m_SpawnCount;
            ring.m_Balls = new EditorBallBase[count];

            float start = (ringNum % 2) * (m_SpawnAngle / 2f);

            for (int i = 0; i < count; i++)
            {
                Vector3 cordinates = Quaternion.Euler(0, 0, start + i * m_SpawnAngle) * new Vector3(radius, 0, 0);
                GameObject obj = Instantiate(m_BallPrefab, cordinates, Quaternion.identity);
                obj.transform.SetParent(ring.transform, false);
                ring.m_Balls[i] = obj.GetComponent<EditorBallBase>();

                //ring.m_Balls[i].transform.localScale = Vector3.one + (.04f * ringNum) * Vector3.one;
                //float delta = i * m_SpawnAngle+80*start;
                float delta = i * m_SpawnAngle;
                int num = (int)(delta / 40f);
                num = num % 5;
                ring.m_Balls[i].SetColor(num);
                //m_ColorCount[num]++;

                //ring.GetComponent<Ring>().m_Candies.Add(obj.GetComponent<Candy>());
            }
        }

        public void UpdateRings()
        {
            float min = 20;
            float max = 80;
            float startRadius = 20;
            //float delta = .3f*startRadius;

            for (int i = 0; i < m_Rings.Length; i++)
            {
                m_Rings[i].m_Radius = startRadius;
                startRadius += 5;
                //delta -= .14f;

                UpdateRingBalls(i);
            }
        }

        public void UpdateRingBalls(int num)
        {
            float m_SpawnAngle = (360f / (float)m_Rings[num].m_Balls.Length);
            int count = m_Rings[num].m_Balls.Length;
            float start = (num % 2) * (m_SpawnAngle / 2f);

            for (int i = 0; i < m_Rings[num].m_Balls.Length; i++)
            {
                Vector3 cordinates = Quaternion.Euler(0, 0, start + i * m_SpawnAngle) * new Vector3(m_Rings[num].m_Radius, 0, 0);
                m_Rings[num].m_Balls[i].transform.localPosition = cordinates;
                m_Rings[num].m_Balls[i].transform.localScale = (.8f + .07f * num) * Vector3.one;

                int colorNum = i % 5;
                m_Rings[num].m_Balls[i].SetColor(colorNum);
            }
        }
        public void Generate()
        {


            float radius = 40;
            //GameObject ring = new GameObject();
            //ring.transform.position = Vector3.zero;
            //ring.AddComponent<Ring>();
            //ring.GetComponent<Ring>().m_Candies = new List<Candy>();

            float m_SpawnCount;
            float m_SpawnAngle;
            m_SpawnCount = (2 * Mathf.PI * radius) / 7;

            m_SpawnAngle = (360 / (float)Math.Floor(m_SpawnCount));

            for (int i = 0; i < Math.Floor(m_SpawnCount); i++)
            {
                Vector3 cordinates = Quaternion.Euler(0, 0, i * m_SpawnAngle) * new Vector3(radius, 0, 0);
                GameObject obj = Instantiate(m_BallPrefab, cordinates, Quaternion.identity);
                //m_Candies.Add(obj.GetComponent<Candy>());
                //obj.GetComponent<Candy>().SetColor(UnityEngine.Random.Range(0, 5));

                //float delta = i * m_SpawnAngle+80*start;
                float delta = i * m_SpawnAngle;
                int num = (int)(delta / 20);
                num = num % 5;
                //obj.GetComponent<Candy>().SetColor(num);
                //m_ColorCount[num]++;
                obj.transform.SetParent(transform, false);
                //ring.GetComponent<Ring>().m_Candies.Add(obj.GetComponent<Candy>());
            }

            //m_CandyRings.Add(ring);
            //ring.GetComponent<Ring>().m_Radius = radius;
        }

        public void Generate2()
        {
            GameObject obj = new GameObject("ball");
            obj.transform.SetParent(transform, false);
        }

        public void ColorBalls(int ringNum, int colorNum)
        {
            for (int i = 0; i < m_Rings[ringNum].m_Balls.Length; i++)
            {
                EditorBallBase ball = m_Rings[ringNum].m_Balls[i];
                float delta = Vector3.SignedAngle(Quaternion.Euler(0, 0, m_StartAngle) * new Vector3(200, 0, 0), ball.transform.localPosition, Vector3.forward);
                if (delta > 0 && delta < m_DeltaAngle)
                {
                    ball.SetColor(colorNum);
                }

            }
        }

        public void RemoveAndReset()
        {
            m_Rings = new EditorRing[1];

        }

        private void OnDrawGizmos()
        {
            //Gizmos.DrawWireSphere(Vector3.zero, m_StartRadius);

            Vector3 pos = Quaternion.Euler(0, 0, m_StartAngle) * new Vector3(80, 0, 0);
            Gizmos.DrawLine(Vector3.zero, pos);
            pos = Quaternion.Euler(0, 0, m_StartAngle + m_DeltaAngle) * new Vector3(80, 0, 0);
            Gizmos.DrawLine(Vector3.zero, pos);
        }
    }
}